package com.shanghaionstar.remote.manager;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;

import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.rxconstants.RxConstants;
import com.onstar.cn.vehicle.result.PollingResult;
import com.shanghaionstar.R;
import com.shanghaionstar.swipe.SwipeActivity;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by 6335yz on 2016/7/24.
 */
@ContentView(R.layout.vehicle_loading_activity)
public class VehicleLoadingActivity extends SwipeActivity {
    @FindViewById
    private Button btn_i_know;

    @Override
    public BasePresenter getPresenter() {
        return null;
    }
    private ObjectAnimator animatorX;
    private Subscription rxSubscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        setTheme(R.style.vehicle_loading_dialog);
        super.onCreate(savedInstanceState);
//        WindowManager.LayoutParams dialog_window_attributes = getWindow().getAttributes();
//        dialog_window_attributes.alpha = 0.5f;
//        DisplayMetrics dm = new DisplayMetrics();
//        getWindowManager().getDefaultDisplay().getMetrics(dm);
//        //设置宽度
//        dialog_window_attributes.width = dm.widthPixels;
//        //设置高度
//        dialog_window_attributes.height = dm.heightPixels;
//        getWindow().setAttributes(dialog_window_attributes);
        final ImageView imageView1 = (ImageView) findViewById(R.id.circle);
        final ImageView imageView2 = (ImageView) findViewById(R.id.circle2);
        final ImageView imageView3 = (ImageView) findViewById(R.id.circle3);
        AnimatorSet composeSet = new AnimatorSet();
        AnimatorSet set1 = getLoadingSet(imageView1);
        AnimatorSet set2 = getLoadingSet(imageView2);
        AnimatorSet set3 = getLoadingSet(imageView3);
        set1.setStartDelay(1000);
        set2.setStartDelay(2000);
        set3.setStartDelay(3000);
        composeSet.playTogether(set1, set2, set3);
        composeSet.setDuration(4000);
        composeSet.start();
        btn_i_know.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        registAllPageAlert();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(animatorX != null){
            animatorX.removeAllListeners();
            animatorX = null;
        }
        if( rxSubscription!= null && !rxSubscription.isUnsubscribed()){
            rxSubscription.unsubscribe();
        }
    }

    private AnimatorSet getLoadingSet(final ImageView imageView) {
        animatorX = ObjectAnimator.ofFloat(imageView, "scaleX", 0f, 1.5f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(imageView, "scaleY", 0f, 1.5f);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0f);
        animatorX.setRepeatCount(-1);//设置动画重复次数
        animatorX.setInterpolator(new DecelerateInterpolator());
        animatorX.setRepeatMode(ValueAnimator.RESTART);//动画重复模式
        animatorX.addListener(new AnimatorListenerAdapter() {
            private boolean hasShow = false;

            @Override
            public void onAnimationStart(Animator animation) {
                if (!hasShow) {
                    hasShow = true;
                    imageView.setVisibility(View.VISIBLE);
                }
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationResume(Animator animation) {
                super.onAnimationResume(animation);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

                super.onAnimationRepeat(animation);
            }
        });
        animatorY.setRepeatCount(-1);//设置动画重复次数
        animatorY.setRepeatMode(ValueAnimator.RESTART);//动画重复模式
        animatorY.setInterpolator(new DecelerateInterpolator());
        alpha.setRepeatCount(-1);//设置动画重复次数
        alpha.setRepeatMode(ValueAnimator.RESTART);//动画重复模式
        alpha.setInterpolator(new DecelerateInterpolator());
        AnimatorSet set = new AnimatorSet();
        set.play(animatorX).with(animatorY).with(alpha);
        return set;
    }

    public void registAllPageAlert() {
       rxSubscription = RxBus.getDefault().toObserverable(RxConstants.ALL_PAGE_ALERT, PollingResult.class)
                .subscribeOn(getScheduler().backgroundThread())
                .observeOn(getScheduler().mainThread()).
                subscribe(new Action1<PollingResult>() {
                              @Override
                              public void call(PollingResult pollingResult) {
                                  finish();
                              }
                          }

                );
    }
}
